/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucuenca.paginaasos.controller.util;

import edu.ucuenca.paginaasos.controller.exceptions.EnvioMailException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Marcelo
 */
public class Mail {

    public static boolean sendFromGmail(final String emailOrigen, final String emailPassword, String asunto, String contenido, String emailDestino) throws EnvioMailException {
        if (emailOrigen.isEmpty() || emailPassword.isEmpty() || asunto.isEmpty() || emailDestino.isEmpty())  {
            return false;
        }
        
        String host = "smtp.gmail.com";
        
        PasswordAuthentication authenticator = new javax.mail.PasswordAuthentication(emailOrigen, emailPassword);

        // Get system properties  
        Properties properties = System.getProperties();

        // Setup mail server  
        properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.port", 465);
        //properties.put("mail.smtp.port", "587");  
        properties.setProperty("mail.smtp.auth", "true");
        //properties.put("mail.smtp.ssl.enable", "true");  
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.username", emailOrigen);
        properties.put("mail.smtp.password", emailPassword);

        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.fallback", "false");

        properties.setProperty("mail.smtp.submitter", authenticator.getUserName());

        Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(emailOrigen, emailPassword);
                    }
                });

        // Get the default Session object.  
        //Session session = Session.getDefaultInstance(properties);  
        session.setDebug(true);

        try {

            // Create a default MimeMessage object.  
            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress(emailOrigen));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailDestino));

            message.setSubject(asunto);

            //message.setText(contenido);

            message.setContent(contenido, "text/html; charset=utf-8");

            Transport.send(message);

            System.out.println("Sent message successfully....");

            return true;
        } catch (MessagingException mex) {
            throw new EnvioMailException(mex);
            //mex.printStackTrace();
        }
        //return false;
    }
}
